package com.springbootshiro.config;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ThymelContro {


    @RequestMapping("/login")
    public String login(String name,String password,Model model){
        /**
         * 使用shiro 编写认证操作
         */
        //1. 获取Subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name,password);
        //3.执行登陆方法
        try{
            //登陆成功
            subject.login(token);
        }catch (UnknownAccountException e){
            //登陆失败
            model.addAttribute("msg","登陆失败,用户名不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(Model model){
        model.addAttribute("name","tian");
        return "login";
    }

    @RequestMapping("/update")
    public String update(Model model){
        model.addAttribute("update","updateuser");
        return "user/update";
    }

    @RequestMapping("/add")
    public String add(Model model){
        model.addAttribute("add","adduser");
        return "user/add";
    }

    @RequestMapping("/noAuth")
    public  String noAuth(){
        return "noAuth";
    }
}
